package com.alignet.gn.service.impl;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alignet.gn.hibernate.TgnRole;
import com.alignet.gn.hibernate.TgnUser;
import com.alignet.gn.hibernate.dao.TgnRoleDAO;
import com.alignet.gn.service.RoleService;
import com.alignet.gn.sesion.Container;
import com.alignet.gn.util.GNUtils;

@Service("RolService")
public class RoleServiceImpl implements RoleService{

	private static final long serialVersionUID = 4089937794197364979L;
	private static final Logger LOG = LoggerFactory.getLogger(RoleServiceImpl.class);
	final transient private  TgnRoleDAO roleDao;
	final transient private Container container;
	
	@Autowired
	public RoleServiceImpl(final Container container,final TgnRoleDAO roleDao) {
		this.container = container;
		this.roleDao=roleDao;
	}
	
	public String eliminaRole(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<TgnRole> listaRoles() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<TgnUser> listaUsuarios(Integer idUser, Integer idRol) {
		// TODO Auto-generated method stub
		return null;
	}

	public TgnRole obtieneRol(Integer idRole) {
		// TODO Auto-generated method stub
		return null;
	}

	public String registerRole(TgnRole rol, String cadena) {
		// TODO Auto-generated method stub
		return null;
	}

	public String roleCreateInput() {
		// TODO Auto-generated method stub
		return null;
	}

	public String updateRole(TgnRole rol, String cadena) {
		// TODO Auto-generated method stub
		return null;
	}

	public String newRole() {
		roleDao.updateRole(1,container);
		return "success";
	}

	@Transactional(readOnly = false)
	public String registerRole(TgnRole role,String cadena,String actionxedit) {
		String rpta = "success";

		LOG.info("Class: RoleServiceImpl - Method: registerRole");
		
		if (GNUtils.esVacioNulo(role.getVcName())) {
			return "Requser1";
		} else if (!GNUtils.esAlfa(role.getVcName().replace(" ", ""))) {
			return "mnsTh21";
		} else if (role.getVcName().length()>60) {
			return "Corruser1";
		}else{
			String actions = cadena;
		
			rpta = roleDao.registraModificaRol(cadena);
			LOG.info("Respuesta : " +rpta);
			if (rpta.equals("success")) {
				BigInteger activated = GNUtils.getXor(actions);
				TgnRole rol = new TgnRole();
				rol.setVcEditable("1");
				rol.setVcGroupidaction(activated.toString());
				rol.setInIdentity(container.getInIdCommerce());
				rol.setVcName(role.getVcName());
				Date d=new Date();
				Timestamp time= new Timestamp(d.getTime());
				rol.setDtRegistry(time);
				rol.setChState("1");
				rol.setVcTypeentity("3");
				rol.setDtUpdate(time);
				rol.setInIduserreg(1);
				rol.setInIduserupd(1);
				roleDao.saveRol(rol);
				container.setActionsxedit(actionxedit);
			}
		}
		return rpta;
	}

	public void obtieneRol(TgnRole rol) {
		roleDao.obtieneRol(rol,container);
	}

	@Transactional(readOnly = false)
	public String updateRole(TgnRole role, String cadena, String actionxedit) {
		String rpta = "success";
		LOG.info("Class: RoleServiceImpl - Method: updateRole");
		String actions = cadena;
		rpta = roleDao.registraModificaRol(cadena);
		if (rpta.equals("success")) {
			BigInteger activated = GNUtils.getXor(actions);
			role.setVcGroupidaction(activated.toString());
			Date d= new Date();
			role.setDtUpdate(new Timestamp(d.getTime()));
			role.setInIduserupd(container.getInIduser());
			roleDao.update(role);
		}

		return rpta;
	}

	@Transactional(readOnly = false)
	public void delete(TgnRole role) {
		roleDao.update(role);
	}

}
